<%--***************************************
*
*易宝支付
* 
***************************************--%>
<%@ page import="
	java.util.*,
	java.math.BigDecimal,
	com.probiz.estore.Constants,
	com.probiz.estore.system.util.TranslateService,
	com.probiz.estore.order.model.OnlinePaymentResponse,
	com.probiz.estore.common.model.system.PaymentGateway,
	com.probiz.estore.system.service.PaymentGatewayManager,
	com.probiz.estore.common.service.OrderService,
	org.springframework.web.context.support.WebApplicationContextUtils,
	org.springframework.web.context.WebApplicationContext"
	pageEncoding="UTF-8"%>
<%
	
	WebApplicationContext webAppContext=WebApplicationContextUtils.getWebApplicationContext(pageContext.getServletContext());
	PaymentGatewayManager paymentGatewayManager=(PaymentGatewayManager)webAppContext.getBean("paymentGatewayManager");
	PaymentGateway paymentGateway=paymentGatewayManager.getByPaymentGatewayCode("yeepay");
	HashMap paramMap = paymentGateway.getConfigData();
	
	//-----------------------start: 账户信息------------------------------------------
	String p1_MerId ="";
	String encryptionKey="";
	if (paramMap != null) {
		p1_MerId = paramMap.get("merchantId").toString().trim(); 			//商户号
		encryptionKey = paramMap.get("encryptionKey").toString().trim(); 	//密钥
	}
	//-----------------------end:   账户信息------------------------------------------
	String r0_Cmd= request.getParameter("r0_Cmd");				//业务类型
	String r1_Code =  request.getParameter("r1_Code");			//支付结果
	String r2_TrxId = request.getParameter("r2_TrxId");			//易宝支付交易流水号
	String r3_Amt = request.getParameter("r3_Amt");				//支付金额
	String r4_Cur = request.getParameter("r4_Cur");				//交易币种
	String r5_Pid = request.getParameter("r5_Pid");				//商品名称
	String r6_Order = request.getParameter("r6_Order");			//商户订单号
	String r7_Uid = request.getParameter("r7_Uid");				//易宝支付会员ID
	String r8_MP = request.getParameter("r8_MP");				//商户扩展信息
	String r9_BType = request.getParameter("r9_BType");			//交易结果返回类型  为“1”: 浏览器重定向; 为“2”: 服务器点对点通讯.
	String hmac = request.getParameter("hmac");					// 签名数据
	
	StringBuffer crackText = new StringBuffer();
	crackText.append(p1_MerId);
	crackText.append(r0_Cmd);
	crackText.append(r1_Code);
	crackText.append(r2_TrxId);
	crackText.append(r3_Amt);
	crackText.append(r4_Cur);
	crackText.append(r5_Pid);
	crackText.append(r6_Order);
	crackText.append(r7_Uid);
	crackText.append(r8_MP);
	crackText.append(r9_BType);

	String hmac_t = TranslateService.hmacMd5Transalte(crackText.toString(),encryptionKey);
	
	Short flag = Constants.PAYMENT_HISTORY_FLAG_FAILURE;
	if (hmac.equals(hmac_t)) {
		BigDecimal amount = new BigDecimal(r3_Amt);
		if (r1_Code.equals("1")) {
			//支付成功
			flag = Constants.PAYMENT_HISTORY_FLAG_SUCCESS;
			if(r9_BType.equals("1")) {
				//out.println("callback方式:产品通用接口支付成功返回-浏览器重定向");
				// 产品通用接口支付成功返回-服务器点对点通讯
			} else if(r9_BType.equals("2")) {
				// 如果在发起交易请求时	设置使用应答机制时，必须应答以"success"开头的字符串，大小写不敏感
				out.println("SUCCESS");
			  // 产品通用接口支付成功返回-电话支付返回		
			}
		} 
		OnlinePaymentResponse onlinePaymentResponse = new OnlinePaymentResponse();
		onlinePaymentResponse.setFlowNo(r2_TrxId);   //必填. 如果支付网关没有流水号，则使用原始的orderNo，即未decode前的
		onlinePaymentResponse.setOrderNo(r6_Order);  //必填
		onlinePaymentResponse.setFlag(flag);    //必填
		onlinePaymentResponse.setAmount(amount);  //必填
		onlinePaymentResponse.setPaymentGateway(paymentGateway);  //必填
		onlinePaymentResponse.setPayer(r7_Uid);
		OrderService orderService = (OrderService) webAppContext.getBean("orderService");
		request.setAttribute("onlinePaymentResponse",onlinePaymentResponse);
		orderService.doHandleOnlinePaymentResponse(onlinePaymentResponse,request);
	} else {
		out.println("交易签名被篡改!");
	}
%>
<%
	if(r9_BType.equals("1")) {
		//浏览器返回
%>
<%@include file="./include/finishPayResult.jspf"%>
<%
	}else if(r9_BType.equals("2")) {
		//异步返回
%>
<head>
	<META name="decorator" content="blank"/>
</head>
<%
	}
%>
